//
//  JRTabBarController.swift
//  swiftWeiBo
//
//  Created by Johnray on 15/9/21.
//  Copyright © 2015年 Johnray. All rights reserved.
//

import UIKit

class JRTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
       
        //添加子控制器
        addChildViewControllers()
        
        //添加button到tabbar
        setBtn()
        
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(btn)
        
    }

    
    
/// 添加子控制器
     func addChildViewControllers() {
        
        addChildViewController(JRHomeViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(JRMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        
        
        addChildViewController(UIViewController(), title: nil, imageName: nil)
        
        addChildViewController(JRDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(JRPersonalViewController(), title: "我", imageName: "tabbar_profile")
        
        
    }
    
    
    func addChildViewController(vc:UIViewController,title:String?,imageName:String?) {
        //tabBar.tintColor = UIColor.orangeColor()
        
        let nav =  UINavigationController(rootViewController: vc)
        
        vc.title = title
        
        if imageName != nil{
        
            vc.tabBarItem.image = UIImage(named: imageName!)
            
        }
        
        
        
        addChildViewController(nav)
        
        
    }
    
    //MARK: - 添加编辑按钮
    func setBtn(){
        
        tabBar.addSubview(btn)
        let count = self.viewControllers?.count
        
        let width = UIScreen.mainScreen().bounds.width
        
        //减1为了让btn完全覆盖在tabbarItem上
        let btnWidth = width / CGFloat(count!) - 1
        
        btn.frame = CGRectInset(tabBar.bounds, btnWidth * 2, 0)
        
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    @objc private func btnClick(){
    
        print("点击了")
    
    }

    //MARK: - 懒加载
    private lazy var btn:UIButton = UIButton(imagename: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    
    
    

}
